package net.jjjshop.common.service.common.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.jjjshop.common.ali.sms.AliYunSmsUtil;
import net.jjjshop.common.cache.RedisUtil;
import net.jjjshop.common.constants.SmsConstants;
import net.jjjshop.common.entity.Sms;
import net.jjjshop.common.entity.user.User;
import net.jjjshop.common.service.common.SmsService;
import net.jjjshop.common.service.user.UserService;
import net.jjjshop.common.util.common.CommonUtil;
import net.jjjshop.framework.common.exception.BusinessException;
import net.jjjshop.framework.shiro.vo.LoginUserRedisVo;
import net.jjjshop.framework.util.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;



@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserService userService;

    @Override
    public Boolean sendCommonCode(Sms sendSmsVo) {
        return sendSms(sendSmsVo);
    }

    /**
     * 发送短信
     * @return boolean
     */
    private Boolean sendSms(Sms sendSmsVo) {
        Long codeExpireTime = SmsConstants.codeExpireTime;
        Integer code = Integer.valueOf(CommonUtil.randomCount(111111, 999999).toString());
       if (sendSmsVo.getType().equals(SmsConstants.SMS_CONFIG_TYPE_VERIFICATION_TYPE)) {// 注册验证码
//            HashMap<String, Object> justPram = new HashMap<>();
//            justPram.put("code", code);
//            justPram.put("time", codeExpireTime);
           String mobile = sendSmsVo.getMobile();
           sendSmsVo.setCode(String.valueOf(code));
            Boolean aBoolean = commonSendSms(sendSmsVo);
            // 将验证码存入redis
            redisUtil.set(SmsConstants.SMS_REGISTER_KEY+""+mobile, code,codeExpireTime, TimeUnit.SECONDS);
            return aBoolean;
       }
        if (sendSmsVo.getType().equals(SmsConstants.SMS_CONFIG_TYPE_RESET_TYPE)) {// 重置验证码
//            HashMap<String, Object> justPram = new HashMap<>();
//            justPram.put("code", code);
//            justPram.put("time", codeExpireTime);
            String mobile = sendSmsVo.getMobile();
            List<User> userList = userService.list(new LambdaQueryWrapper<User>()
                    .eq(User::getMobile, mobile)
                    .eq(User::getAppId, 10001));
            if (null == userList || userList.isEmpty()){
                throw new BusinessException("未找到用户信息");
            }
            sendSmsVo.setCode(String.valueOf(code));
            Boolean aBoolean = commonSendSms(sendSmsVo);
            // 将验证码存入redis
            redisUtil.set(SmsConstants.SMS_RESET_KEY+""+mobile, code,codeExpireTime, TimeUnit.SECONDS);
            return aBoolean;
        }
       //设置支付密码
       if (sendSmsVo.getType().equals(SmsConstants.SMS_CONFIG_TYPE_SETPAYPASS_TYPE)) {
           LoginUserRedisVo vo = LoginUtil.getLoginUserRedisVo();
           User user = userService.getById(vo.getUserId());
           if(user == null){
               throw new BusinessException(-1, "未找到用户信息");
           }
           String mobile = user.getMobile();
           sendSmsVo.setMobile(mobile);
           sendSmsVo.setCode(String.valueOf(code));
           Boolean aBoolean = commonSendSms(sendSmsVo);
           // 将验证码存入redis
           redisUtil.set(SmsConstants.SMS_SETPAYPASS_KEY+""+mobile, code,codeExpireTime, TimeUnit.SECONDS);
           return aBoolean;
        }

        throw new BusinessException("发送短信失败");
     //  return false;
       // return commonSendSms(sendSmsVo);
    }
    /**
     * 公共发送短信
     *
     * @param sendSmsVo 发送短信对象
     * @return 是否发送成功
     */
    private Boolean commonSendSms(Sms sendSmsVo) {
        try {
            Boolean aBoolean = AliYunSmsUtil.sendSms(sendSmsVo.getMobile(), Long.valueOf(sendSmsVo.getCode()));
            if (aBoolean){
                return true;
            }
            return false;

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("发送短信失败：" + e.getMessage());
            return false;
        }
    }

}
